$(function () {
    $("#orgDepartmentGrid").Grid({
        url: '../org/department/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '部门名称', name: 'name', index: 'name', width: 80},
			{label: '所属公司', name: 'companyName', index: 'company_name', width: 80},
			{label: '排序', name: 'orderNum', index: 'order_num', width: 80}]
    });
    vm.getDepart();
});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el: '#orgDepartment',
	data: {
        showList: true,
        title: null,
		orgDepartment: {},
		departList: [],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',companyName:''
		}
	},
	methods: {
		queryDepartList:function(val){
			if(val){
				var addResult=true;
				for(var i=0;i<vm.departList.length;i++){
					if(vm.departList[i].dictValue==val){
						addResult=false;
					}
				}
				if(addResult){
					vm.departList.push({'dictKey':val,'dictValue':val});
				}
			}
		},
		getDepart: function () {
            Ajax.request({
                url: '../sys/dict/queryAll',
                params: {
                    'groupCode': 'ORG_DEPART'
                },
                async: true,
                successCallback: function (r) {
                    vm.departList = r.list;
                }
            });
        },
        getCompany: function () {
            //加载部门树
            Ajax.request({
                url: "../org/company/select",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#orgCompanyTree"), setting, r.deptList);
                    var node = ztree.getNodeByParam("deptId", vm.orgDepartment.companyId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.orgDepartment.companyName = node.name;
                    } else {
                        node = ztree.getNodeByParam("deptId", '01');
                        ztree.selectNode(node);
                        vm.orgDepartment.companyName = node.name;
                    }
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.orgDepartment = {companyName:'',companyId:''};
			vm.getCompany();
		},
		update: function (event) {
            var id = getSelectedRow("#orgDepartmentGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            var url = vm.orgDepartment.id == null ? "../org/department/save" : "../org/department/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.orgDepartment),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
        deptTree: function () {
            openWindow({
                title: "选择上级",
                area: ['300px', '450px'],
                content: jQuery("#orgCompanyLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.orgDepartment.companyId = node[0].deptId;
                    vm.orgDepartment.companyName = node[0].name;

                    layer.close(index);
                }
            });
        },
		del: function (event) {
            var ids = getSelectedRows("#orgDepartmentGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../org/department/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../org/department/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.orgDepartment = r.orgDepartment;
                    vm.getCompany();
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var page = $("#orgDepartmentGrid").jqGrid('getGridParam', 'page');
			$("#orgDepartmentGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'companyName':vm.q.companyName},
                page: 1
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: '',companyName:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});